import 'dart:io';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import '../provied/cartmodel.dart';
import 'package:provider/provider.dart';
class ImagePickerPage extends StatefulWidget {
  ImagePickerPage({Key key}) : super(key: key);

  _ImagePickerPageState createState() => _ImagePickerPageState();
}

class _ImagePickerPageState extends State<ImagePickerPage> {
  @override
  void initState(){
    super.initState();
  }
  //记录选择的照片
  File _image;

  //当图片上传成功后，记录当前上传的图片在服务器中的位置
  String _imgServerPath;
  String images;
  //拍照
  Future _getImageFromCamera() async {
    var image =
        await ImagePicker.pickImage(source: ImageSource.camera, maxWidth: 400);

    setState(() {
      _image = image;
    });
  }

  //相册选择
  Future _getImageFromGallery() async {
    var image = await ImagePicker.pickImage(source: ImageSource.gallery);

    setState(() {
      _image = image;
       uploadFile(_image);
      print(_image);
    });
  }

  //上传图片到服务器
  _uploadImage() async {
    //context.read()
    // print('图片上传${_image.path}');
    //  String path = _image.path;
    // var name = path.substring(path.lastIndexOf("/") + 1, path.length);
    // FormData formData=new FormData.fromMap({

    // });
    // print(formData);
  }
  Future uploadFile(File file)async{
    String path=file.path;
     var fileData = await MultipartFile.fromFile(
      path
    );
    FormData formData = FormData.fromMap({
      "file": fileData,
    });
    //print();
     Response response;
     Dio dio=new Dio();
     response =await Dio().post("http://dbyxs.xyz:3005/img/", data: formData);
     images=response.data['data'];
    print(images);
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("请选择或上传图片")),
      body: Container(
        child: ListView(
          children: <Widget>[
            // RaisedButton(
            //   onPressed: () {
            //    // uploadFile();
            //    // _getImageFromCamera();
            //   },
            //   child: Text("照相机"),
            // ),
            SizedBox(height: 10),
            RaisedButton(
              onPressed: () {
                _getImageFromGallery();
              },
              child: Text("从相册中选择"),
            ),
            SizedBox(height: 10),
            /**
             * 展示选择的图片
             */
            _image == null
                ? Text("no image selected")
                : Image.file(
                    _image,
                    fit: BoxFit.cover,
                  ),
            // SizedBox(height: 10),
            RaisedButton(
              onPressed: () {
                setState(() {
                   context.read<CartProvide>().getdetailamin(images);
                });
                _uploadImage();
              },
              child: Text("确认修改"),
            ),
            SizedBox(height: 10),
            // _imgServerPath == null
            //     ? Text("没有上传图片")
            //     : Image.network(
            //         "http://jd.itying.com/public/upload/daoyXVTvrCCUeoIliZtNXX-s.png"),
          ],
        ),
      ),
    );
  }
}